//
//  DetailReportView.m
//  BabyHelper
//
//  Created by  thomas on 11-2-12.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "DetailReportView.h"


@implementation DetailReportView
@synthesize reportMode,startTime,endTime,reportEntry;

- (void)viewDidLoad {
    self.title = NSLocalizedString(@"Detail Report", @"");
	self.reportEntry = [[BabyActivityReportEntry alloc]init];
	[super viewDidLoad];
	
	//set background image
	self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background"]];
	self.view.backgroundColor = [UIColor clearColor];
	//set navigation background color
	self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:navBarRed green:navBarGreen blue:navBarBlue alpha:navBarAlpha];
	
	UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]
								  initWithImage:[UIImage imageNamed:@"sina"]
								  style:UIBarButtonItemStylePlain 
								  target:self
								  action:@selector(toggleShare)		
								  ];
	//rightItem.image = [UIImage imageNamed:@"sina"];
	self.navigationItem.rightBarButtonItem = rightItem;
	[rightItem release];
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
	Dao *dao = [Dao getInstance];
	[dao GetReportSummaryWithStartEndDate:self.startTime endDate:self.endTime report:self.reportEntry];
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
	switch (indexPath.row) {
		case 0:
			cell.textLabel.text = [NSString stringWithFormat:NSLocalizedString(@"Mum       %d times       %d min ",@""),self.reportEntry.countOfEatMum,reportEntry.amountOfEatMum];
			cell.imageView.image = [UIImage imageNamed:@"mum"];
			break;
		case 1:
			cell.textLabel.text = [NSString stringWithFormat:NSLocalizedString(@"Milk       %d times       %d ml",@""),self.reportEntry.countOfEatMilk,reportEntry.amountOfEatMilk];
			cell.imageView.image = [UIImage imageNamed:@"milk_bottle"];
			break;
		case 2:
			cell.textLabel.text = [NSString stringWithFormat:NSLocalizedString(@"Water       %d times       %d ml",@""),self.reportEntry.countOfDrinkWater,reportEntry.amountOfDrinkWater];
			cell.imageView.image = [UIImage imageNamed:@"DrinkWater"];
			break;
		case 3:
			cell.textLabel.text = [NSString stringWithFormat:NSLocalizedString(@"Pee       %d times",@""),self.reportEntry.countOfPee];
			cell.imageView.image = [UIImage imageNamed:@"pee"];
			break;
		case 4:
			cell.textLabel.text = [NSString stringWithFormat:NSLocalizedString(@"Shit       %d times",@""),self.reportEntry.countOfShit];
			cell.imageView.image = [UIImage imageNamed:@"shit"];
			break;
		default:
			break;
	}
	
	cell.backgroundColor = [UIColor colorWithRed:cellBackgroundReportRed green:cellBackgroundReportGreen 
											blue:cellBackgroundReportBlue alpha:cellBackgroundReportAlpha];

    return cell;
}

- (void)dealloc {
    [super dealloc];
}

-(void)toggleShare{
	BabyHelperAppDelegate *appDelegate = (BabyHelperAppDelegate *)[[UIApplication sharedApplication] delegate];
	UIViewController *controller = [OAuthController controllerToEnterCredentialsWithEngine: appDelegate._engine delegate: self];
	if (controller) 
	{
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Alert",@"") 
														message:NSLocalizedString(@"You are not log in yet", @"")
													   delegate:nil 
											  cancelButtonTitle:@"OK"
											  otherButtonTitles:nil];
		[alert show];
		[alert release];
	}
	else {
		NSLog(@"Authenicated for %@..", appDelegate._engine.username);
		[OAuthEngine setCurrentOAuthEngine:appDelegate._engine];
		ComposeViewController *composeViewController = [[ComposeViewController alloc]init];
		[self presentModalViewController:composeViewController animated:YES];
		composeViewController.messageTextField.text = [self GetReportContent];
		[composeViewController newTweet];					
	}				
}

- (NSString *)GetReportContent{
	
	Dao *dao = [Dao getInstance];
	BabyProfile *babyProfile = [[BabyProfile alloc]init];
	[dao LoadBabyProfile:babyProfile];
	
	NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
	[dateFormatter setDateFormat:DATE_TIME_FORMAT_NO_YEAR];
	
    NSString *report = [NSString stringWithFormat:BABY_ACTIVITY_REPORT_FORMAT_STR,
																	babyProfile.Name,
																	[dateFormatter stringFromDate:self.startTime],
																	[dateFormatter stringFromDate:self.endTime],						
																	self.reportEntry.countOfEatMum,
																	self.reportEntry.amountOfEatMum,
																	self.reportEntry.countOfEatMilk,
																	self.reportEntry.amountOfEatMilk,
																	self.reportEntry.countOfDrinkWater,
																	self.reportEntry.amountOfDrinkWater,
																	self.reportEntry.countOfPee,
																	self.reportEntry.countOfShit];
	[babyProfile release];	
	[dateFormatter release];
	return report;
}
- (void) OAuthController: (OAuthController *) controller authenticatedWithUsername: (NSString *) username{
    [[controller.engine requestToken] storeInUserDefaultsWithServiceProviderName:@"SINA" prefix:@"REQUEST_TOKEN"];   
	[[controller.engine accessToken] storeInUserDefaultsWithServiceProviderName:@"SINA" prefix:@"ACCESS_TOKEN"];   
}
- (void) OAuthControllerFailed: (OAuthController *) controller{
}
- (void) OAuthControllerCanceled: (OAuthController *) controller{
}
@end

